package net.yuyoo.uusession;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpLocalableUUSessionAdapter implements HttpSession {
	
	private static final Log log = LogFactory.getLog(HttpLocalableUUSessionAdapter.class); 
	
	private HttpSession hostSession = null;
 	
	private UUSession uuSession = null; 
	
	private UULocalSession localSession = null;
	
	private boolean sticked = false;
	 			
	public HttpLocalableUUSessionAdapter(HttpSession hostSession, UUSession uuSession, boolean sticked) {
		
		this.hostSession = hostSession;		 
		this.localSession = UULocalSession.getLocalSession(uuSession);
		this.uuSession = uuSession;
		this.sticked = sticked;

		if (log.isDebugEnabled()) {
			log.debug("HttpLocalableUUSessionAdapter[HttpSession <--> UUSession <--> UULocalSession]成功, Stick Flag:" + this.sticked); 
		}				 
		
	}

	/**
	 * 获取属性
	 */
	public Object getAttribute(String arg) {
		
		if (null == arg) {
			return null;
		} 
		if (this.sticked) {
			return this.localSession.cache.get(arg);
		} else {
			return this.uuSession.getAttribute(arg);
		} 
	}

	/**
	 * 获取属性名称枚举
	 */
	public Enumeration getAttributeNames() {  
		
		if (this.sticked) {
			return this.localSession.getAttributeNames();
		} else {
			return this.uuSession.getAttributeNames();	
		}
	}

	/**
	 * 获取创建时间
	 */
	public long getCreationTime() { 
		
		 if (this.sticked) {
			 return this.localSession.getCreateTime();
		 } else {
			 return this.uuSession.getCreateTime();
		 }
	}

	/**
	 * 获取ID
	 */
	public String getId() { 

		if (this.sticked) {
			return this.localSession.getId();
		} else {
			return this.uuSession.getId();
		}
	}

	/**
	 * 获取最后访问时间
	 */
	public long getLastAccessedTime() { 
		
		return this.hostSession.getLastAccessedTime();
	}

	/**
	 * 获取最大可以交互时间
	 */
	public int getMaxInactiveInterval() {
 
		if (this.sticked) {
			return this.localSession.getMaxInactiveInterval();
		} else {
			return this.uuSession.getMaxInactiveInterval();
		}
	}

	public ServletContext getServletContext() { 
		
		return this.hostSession.getServletContext();
	}

	public HttpSessionContext getSessionContext() { 
		
		throw new RuntimeException("调用未支持方法");
	}

	/**
	 * replace by getAttribute
	 */
	public Object getValue(String arg) { 
		
		if (this.sticked) {
			return this.localSession.getAttribute(arg);
		} else {
			return this.uuSession.getAttribute(arg);
		} 
	}

	/**
	 * replace by getAttributeNames
	 */
	public String[] getValueNames() {  

		throw new RuntimeException("调用未支持方法");
	}

	/**
	 * 将UUSession设置无效
	 */
	public void invalidate() {  
		
		this.hostSession.invalidate();
		this.uuSession.invalidate();
		this.localSession.invalidate();
	}

	public boolean isNew() { 
		
		if (this.sticked) {
			return this.localSession.isNew();
		} else {
			return this.uuSession.isNew();
		} 
	}

	public void putValue(String key, Object val) {  
		
		setAttribute(key, val);
	}

	/**
	 * 删除一个属性.
	 */
	public void removeAttribute(String key) { 
		
		this.uuSession.removeAttribute(key);
		if (this.sticked) {
			this.localSession.removeAttribute(key);
		}
	}

	public void removeValue(String key) { 
		
		removeAttribute(key);
	}

	/**
	 * 新增属性
	 */
	public void setAttribute(String key, Object val) { 

		this.uuSession.setAttribute(key, val);	
		if (this.sticked) {
			this.localSession.setAttribute(key, val);
		}
	}

	/**
	 * @deprecated -- 在UUSession 1.0, 未实现此方法.
	 */
	public void setMaxInactiveInterval(int arg) {

		throw new RuntimeException("调用未支持方法");
	} 
}
